package com.ipay.wechat.component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ipay.core.PaySystem;
import com.ipay.utils.HttpClientUtils;
import com.ipay.utils.StringUtil;

@Component
public class Wechat {
	@Value("${wechat.baseurl}")
	private String baseUrl;
	@Value("${wechat.baseurl.http}")
	private String baseUrlHttp;
	@Value("${wechat.token}")
	private String token;
	@Value("${wechat.appid}")
	private String appId;
	@Value("${wechat.secret}")
	private String secret;
	@Value("${wechat.aeskey}")
	private String encodingAesKey;
	// 商户申请结果通知
	@Value("${wechat.TEMPLATE1}")
	private String TEMPLATE1;
	//支付成功通知
	@Value("${wechat.TEMPLATE2}")
	private String TEMPLATE2;
	//退款通知
	@Value("${wechat.TEMPLATE3}")
	private String TEMPLATE3;
	@Value("${alipay.appid}")
	private String alipayappid;
	@Value("${alipay.key}")
	private String alipaykey;
	@Value("${alipay.publickey}")
	private String alipaypublickey;
	//#图片上传路径
	@Value("${upload.img.path}")
	private String uploadImgPath;

	@Value("${wechat.baseurl.wechat}")
	private String baseUrlWechat;

	public String getTEMPLATE2() {
		return TEMPLATE2;
	}

	public void setTEMPLATE2(String tEMPLATE2) {
		TEMPLATE2 = tEMPLATE2;
	}

	public String getTEMPLATE3() {
		return TEMPLATE3;
	}

	public void setTEMPLATE3(String tEMPLATE3) {
		TEMPLATE3 = tEMPLATE3;
	}

	public String getTEMPLATE1() {
		return TEMPLATE1;
	}

	public void setTEMPLATE1(String tEMPLATE1) {
		TEMPLATE1 = tEMPLATE1;
	}

	public String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getSecret() {
		return secret;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public String getEncodingAesKey() {
		return encodingAesKey;
	}

	public void setEncodingAesKey(String encodingAesKey) {
		this.encodingAesKey = encodingAesKey;
	}

	/**
	 * 获取 alipayappid
	 * 
	 * @return alipayappid alipayappid
	 */
	public String getAlipayappid() {
		return alipayappid;
	}

	/**
	 * 设置alipayappid
	 * 
	 * @param alipayappid
	 *            alipayappid
	 */
	public void setAlipayappid(String alipayappid) {
		this.alipayappid = alipayappid;
	}

	/**
	 * 获取 alipaykey
	 * 
	 * @return alipaykey alipaykey
	 */
	public String getAlipaykey() {
		return alipaykey;
	}

	/**
	 * 设置alipaykey
	 * 
	 * @param alipaykey
	 *            alipaykey
	 */
	public void setAlipaykey(String alipaykey) {
		this.alipaykey = alipaykey;
	}

	/**
	 * 获取 alipaypublickey
	 * 
	 * @return alipaypublickey alipaypublickey
	 */
	public String getAlipaypublickey() {
		return alipaypublickey;
	}

	/**
	 * 设置alipaypublickey
	 * 
	 * @param alipaypublickey
	 *            alipaypublickey
	 */
	public void setAlipaypublickey(String alipaypublickey) {
		this.alipaypublickey = alipaypublickey;
	}

	public String getBaseUrlHttp() {
		return baseUrlHttp;
	}

	public void setBaseUrlHttp(String baseUrlHttp) {
		this.baseUrlHttp = baseUrlHttp;
	}

	public String getUploadImgPath() {
		return uploadImgPath;
	}

	public void setUploadImgPath(String uploadImgPath) {
		this.uploadImgPath = uploadImgPath;
	}

	public String getBaseUrlWechat() {
		return baseUrlWechat;
	}

	public void setBaseUrlWechat(String baseUrlWechat) {
		this.baseUrlWechat = baseUrlWechat;
	}

	public String cacheToken() throws IOException {
		String token = "";
		Map<String, String> params = new HashMap<String, String>();
		params.put("grant_type", "client_credential");
		params.put("appid", getAppId());
		params.put("secret", getSecret());
		String result = HttpClientUtils.getResponseText(baseUrl, params);
		Map<String, String> map = JSON.parseObject(result, Map.class);
		if (StringUtil.isNotEmpty(map.get("errCode"))) {
			PaySystem.get().error(map.get("errCode"));
		}
		// TODO 缓存token
		return token;
	}

}
